$(function(){
    var getParams = getArgs();
    var currentHotel = getParams.hotel;
    var orderNumber = getParams.no;
    var symbols = $('#doc').data('symbols');
    toastr.options = {
        "newestOnTop": false,
        "timeOut": 1500,
        "extendedTimeOut": 0,
        "escapeHtml": true,
        "positionClass": "toast-bottom-center"
    };      
    //提交订单
    var requireLoginErrorHandler = function (error) {
        toggleLoading(false);
        if (typeof error.responseJSON === 'undefined') {
            toastr.error(lang('tip.system'));
            return;
        }

        if (error.responseJSON.status === 304) {
            $('#order-list-toggle').addClass('hidden');
            $('#doc-body').addClass('hidden');
            $('#doc-content').removeClass('hidden');
            return;
        }
        toastr.error(error.responseJSON.info);
    };    
    // 判断是否微信浏览器,进行按钮隐藏
    var isWechatBrowser = function() {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            return true;
        } else {
            return false;
        }
    };
    var requestOrderInfo = function () {
        toggleLoading(true);
        ajaxRequest('/Home/Order/detail', {
            'orderNumber': orderNumber
        }, 'post', function (data) {
            toggleLoading(false);  
            var orderInfo = data.data;
            if(parseInt(orderInfo.status) == 1){
                window.location.href = "/Home/OrderPage/index?hotel=" + currentHotel + "&no=" + orderNumber;
            }
            window.orderHotel = orderInfo.orderHotel;
            window.couponGetNo = orderInfo.couponGetNo;
            $('.order-number').text(orderInfo.orderNumber);
            $('.order-hotel').text(orderInfo.hotelName);
            $('.order-price').text(symbols+orderInfo.payPagePrice);
            $('.order-room').text(orderInfo.roomTypeName);
            $('.order-room-num').text(orderInfo.roomName);
            var orderCheckinDate = $('.order-checkin-date');
            var orderNight = $('.order-night');
            if (orderInfo.hour > 0) {
                orderCheckinDate.html(orderInfo.startDate.substr(11));
                orderNight.html(orderInfo.hour + lang('hour'));
            } else if (orderInfo.month > 0) {
                orderCheckinDate.html(orderInfo.startDate.substr(0,10));
                orderNight.html(orderInfo.month + lang('month'));
            } else {
                orderCheckinDate.html(orderInfo.startDate.substr(0,10));
                orderNight.html(orderInfo.night + lang('nights'));
            }            
            
            //后台请求客栈支付信息,判断显示隐藏按钮
            var params = {
                //hotel: currentHotel,
                hotel: window.orderHotel,
            };
            ajaxRequest('/Home/Hotel/payInfo', params, 'POST', function(data) {
                $('.wechatPayButton').removeClass('hidden');
                 $('.aliPayButton').removeClass('hidden');
                 $('.vipcardPayButton').removeClass('hidden');
                if (data.data.wechat == '0' || !isWechatBrowser()) {
                    $('.wechatPayButton').addClass('hidden');
                }
                if ($.cookie('qrclient_ali_and_wechat') != undefined && $.cookie('qrclient_ali_and_wechat') == 1 && data.data.wechat == '1' && data.data.uclbrtWechat == '1') {
                    $('.wechatPayButton').removeClass('hidden');
                }
                if (data.data.ali == '0') {
                    $('.aliPayButton').addClass('hidden');
                }
                if (data.data.vipcardpay == '0') {
                    $('.vipcardPayButton').addClass('hidden');
                }
                var pay = $('#doc .pay:not(.hidden)');
                if(pay.length>0){
                    pay.eq(0).find('.nowrap-content').addClass('check-pay');
                    $('#paybtn').removeAttr('disabled');
                } 
            }, function(error) {});    
            $('#doc .pay').each(function () {
                var link = $(this);
                var originalHref = link.attr('href');
                if (link.hasClass('wechatPayButton')) {
                    ajaxRequest('/Home/Hotel/wechatPayInfo', {
                        //'hotel': currentHotel
                        'hotel': window.orderHotel
                    }, 'post', function (data) {
                        var params = 'type=mz&hotel=' + currentHotel + '&no=' + orderNumber + '&orderHotel=' + window.orderHotel + '&back=/Home/PayPage/speedpay.html&pay=0';
                        if (data.data.ai != '') {
                            params = params + '&ai=' + data.data.ai + '&asc=' + data.data.asc;
                            
                        }
                        link.attr('href', originalHref + params);
                    }, function() {});
                } else {
                    link.attr('href', originalHref + '&hotel=' + currentHotel + '&no=' + orderNumber + '&orderHotel=' + window.orderHotel);
                }
            });            
        }, requireLoginErrorHandler);
    };    
    requestOrderInfo();    

    $('#doc').on('click','.pay',function(){
        $(this).find('.nowrap-content').addClass('check-pay');
        $(this).siblings('.pay').find('.nowrap-content').removeClass('check-pay');
    });    
    

});
